package aop.spring_aop_aspect.spring;

import java.beans.PropertyEditorSupport;

/**
 * Created by IntelliJ IDEA.
 * User: anzhen
 * Date: 14-8-9
 * Time: ����6:19
 * To change this template use File | Settings | File Templates.
 */
public class PhoneEditor extends PropertyEditorSupport{


    @Override
    public void setAsText(String s) throws IllegalArgumentException {

        String stripped = stripNonNumeric(s);
        String areaCode = stripped.substring(0,3);
        String prefix = stripped.substring(3,6);
        String number = stripped.substring(6);
        PhoneNumber  phoneNumber = new PhoneNumber(areaCode,prefix,number);
        this.setValue(phoneNumber);


    }

    private String stripNonNumeric(String original){

        StringBuffer sb = new StringBuffer();
        for(int i=0;i<original.length();i++){

            char c = original.charAt(i);
            if(Character.isDigit(c)){

                sb.append(c);

            }



        }
          return sb.toString();

    }

}
